import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.*;

public class DrawingMenuItemListener implements ActionListener{

	private String text;
	private MultiDraw2 md;
	
	DrawingMenuItemListener(MultiDraw2 md, String text){
		this.text = text;
		this.md = md;
	}

	public void actionPerformed(ActionEvent e) {
		String fname = "";
		if(text.equals("<Show All>")){
			md.open_delete_download_Frame.setVisible(true);
		}else if(text.contains(".ser")){
			System.out.println(md.open0_delete1_download2_toggle);
			if(md.open0_delete1_download2_toggle == 0){
				if(confirm()){
					md.open_delete_download_Frame.dispose();
					md.open_delete_download_Frame.setVisible(false);
					md.mdUser.remoteOut.println("<<OPENDRAWING>>Drawings/"+text);
				}
			}else if(md.open0_delete1_download2_toggle == 1){
				String delFile = text;
				if(confirmDelete(delFile)){
					md.open_delete_download_Frame.dispose();
					md.open_delete_download_Frame.setVisible(false);
					md.mdUser.remoteOut.println("<<DELETEFILE>>Drawings/"
							+delFile);
				}
			}else if(md.open0_delete1_download2_toggle == 2){
				String downFile = text;
					md.open_delete_download_Frame.dispose();
					md.open_delete_download_Frame.setVisible(false);
					md.mdUser.remoteOut.println("<<DOWNLOADFILE>>Drawings/"
							+downFile);
			}
		}else if(text.equals("openButton")){
			if(confirm()){
			md.open_delete_download_Frame.dispose();
			md.open_delete_download_Frame.setVisible(false);
			md.mdUser.remoteOut.println("<<OPENDRAWING>>Drawings/"
					+md.open_delete_download_Box.getSelectedItem().toString());
			}
		}else if(text.equals("cancelButton")){
			md.open_delete_download_Frame.dispose();
			md.open_delete_download_Frame.setVisible(false);
		}else if(text.equals("NEW")){
			if(confirm()){
				md.mdUser.remoteOut.println("<<NEWDRAWING>>");
			}
			
		}else if(text.equals("save")){
			//get fileName from Server
			md.mdUser.remoteOut.println("<<GETFILENAME>>");
			Object[] options = { "YES", "NO" };
			int i = JOptionPane.showOptionDialog(null, 
					"Remember: If you save with a file name already in use, \n"
					+"the old file will be overwritten."
					+"\nAre you sure you want to proceed?", 
					"Warning",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
			            null, options, options[0]);
			//code to set fileName
			if(i==0){
				String fileName = (String) JOptionPane.showInputDialog(null,"What would you like to name your drawing?",
						"Save", JOptionPane.PLAIN_MESSAGE, null, null, md.mdUser.getFileName());
				if(!fileName.endsWith(".ser")){
					fileName+=".ser";
				}
				md.mdUser.remoteOut.println("<<SAVEFILE>>"+fileName);
			}
		}else if(text.equals("deleteButton")){
			String delFile = "";
			if(confirmDelete(delFile)){
				md.open_delete_download_Frame.dispose();
				md.open_delete_download_Frame.setVisible(false);
				md.mdUser.remoteOut.println("<<DELETEFILE>>Drawings/"
						+md.open_delete_download_Box.getSelectedItem().toString());
			}
		}else if(text.equals("downloadButton")){
			String downFile = "";
			md.open_delete_download_Frame.dispose();
			md.open_delete_download_Frame.setVisible(false);
			md.mdUser.remoteOut.println("<<DOWNLOADFILE>>Drawings/"
					+md.open_delete_download_Box.getSelectedItem().toString());
		}else if(text.equals("upload")){
			JFileChooser chooser = new JFileChooser();
			String[] ext = new String[1];ext[0] = "ser";
			FNameSuffixFilter fnsf = new FNameSuffixFilter(".ser",ext);
			chooser.addChoosableFileFilter(fnsf);
			chooser.showOpenDialog(md);
			File f = chooser.getSelectedFile();
			if(f.getName().endsWith(".ser")){
				md.mdUser.rt.uploadFile = f;
				md.mdUser.remoteOut.println("<<UPLOADFILE>>"+f.getName());
			}
		}
	}
	protected boolean confirm(){
		if(md.ShapeObjects.isEmpty()){
			return true;
		}
		Object[] options = { "YES", "NO" };
		
		int i = JOptionPane.showOptionDialog(null, 
				"Changes to your current drawing will be lost if you have not saved them."
				+"\nAre you sure you want to proceed?", 
				"Warning",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
		            null, options, options[0]);
		if(i ==0){
			return true;
		}else{
			return false;
		}
	}
	protected boolean confirmDelete(String delFile){
		Object[] options = { "YES", "NO" };
		int i = JOptionPane.showOptionDialog(null, 
				"Once deleted, your file cannot be retrieved."
				+"\nAre you sure you want to delete "+delFile+"?", 
				"Warning",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
		            null, options, options[0]);
		if(i ==0){
			return true;
		}else{
			return false;
		}
	}
}
